﻿using System;
using System.Data;
using Pantry.Business;
using Pantry.Commons;
using System.Web.UI;
namespace Pantry.Web.Users
{
    /// <summary>
    /// 
    /// </summary>
    public partial class WebForm1 : Page
    {
        private GovOfficeRegionBus _govOfficeRegionBus;
        private static DataView _dataView;
        private static bool _isTrue = true;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            CollectionPager.DataSourcePaged.CurrentPageIndex = 1;
            Page.Title = "View list Goverment office region";
            //if (!IsPostBack)
            //{
                FillterGovOfficeRegionByName(CheckBoxIncludeActivate.Checked);
            //}

        }
        /// <summary>
        /// Fillters the name of the gov office region by.
        /// </summary>
        /// <param name="includeInActived">if set to <c>true</c> [include in actived].</param>
        private void FillterGovOfficeRegionByName(bool includeInActived)
        {
            try
            {
                _govOfficeRegionBus = new GovOfficeRegionBus();
                DataTable listAllGovOfficeRegion = _govOfficeRegionBus.SelectAllGovOfficeRegion();
                _dataView = listAllGovOfficeRegion.DefaultView;
                if (!includeInActived)
                {
                    _dataView.RowFilter = "IsActivated = 'True'";
                    _dataView.Sort = "GovOfficeRegionName ASC";
                }

                CollectionPager.PageSize = ConstantSetup.GovOfficeRegionRowPerPage;
                CollectionPager.BindToControl = RepeaterListGovOfficeRegion;
                CollectionPager.DataSource = _dataView;
                RepeaterListGovOfficeRegion.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListGovOfficeRegion.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }
            
        }

        /// <summary>
        /// Fillters the name of the gov office region by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="includeInActived">if set to <c>true</c> [include in actived].</param>
        private void FillterGovOfficeRegionByName(string name,bool includeInActived)
        {
            try
            {
                _govOfficeRegionBus = new GovOfficeRegionBus();
                string condition = "";
                if (name.Length > -1)
                {
                    for (int i = 0; i < name.Length; i++)
                    {
                        if (i == name.Length - 1)
                        {
                            if (!includeInActived)
                            {
                                condition += "GovOfficeRegionName LIKE '" + name.Substring(i - 1, 1) + "*') AND IsActivated ='True'";
                            }
                            else
                            {
                                condition += "GovOfficeRegionName LIKE '" + name.Substring(i - 1, 1) + "*')";
                            }
                        }
                        else if (i == 0)
                        {
                            condition += "(";
                        }
                        else
                        {
                            condition += "GovOfficeRegionName LIKE '" + name.Substring(i - 1, 1) + "*' OR ";
                        }
                    }
                    _dataView.RowFilter = condition;
                }
                CollectionPager.PageSize = ConstantSetup.GovOfficeRegionRowPerPage;
                CollectionPager.BindToControl = RepeaterListGovOfficeRegion;
                CollectionPager.DataSource = _dataView;
                RepeaterListGovOfficeRegion.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListGovOfficeRegion.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }
        }

        /// <summary>
        /// Checks the box include activate changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxIncludeActivateChanged(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName(CheckBoxIncludeActivate.Checked);
        }

        /// <summary>
        /// Links the group all click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroupAllClick(object sender,EventArgs e)
        {
            FillterGovOfficeRegionByName(CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group0 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup0Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("0123456789", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group1 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup1Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("BACDE", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group2 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup2Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("FGHIJ", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group3 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup3Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("KLMN", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group4 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup4Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("OPQR", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group5 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup5Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("STUV", CheckBoxIncludeActivate.Checked);
        }
        /// <summary>
        /// Links the group6 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup6Click(object sender, EventArgs e)
        {
            FillterGovOfficeRegionByName("WXYZ", CheckBoxIncludeActivate.Checked);
        }

        /// <summary>
        /// Links the sort by name click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkSortByNameClick(object sender,EventArgs e)
        {
            Sort("GovOfficeRegionName");
        }
        /// <summary>
        /// Links the sort by description click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkSortByDescriptionClick(object sender, EventArgs e)
        {
            Sort("Description");
        }
        /// <summary>
        /// Links the sort by county click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkSortByCountyClick(object sender, EventArgs e)
        {
            Sort("CountyName");
        }
        /// <summary>
        /// Links the sort by activated click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkSortByActivatedClick(object sender, EventArgs e)
        {
            Sort("IsActivated");
        }
        /// <summary>
        /// Sorts the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        private void Sort(string name)
        {
            if (_isTrue)
            {
                _dataView.Sort = string.Format("{0} DESC",name);
                _isTrue = false;
            }
            else
            {
                _dataView.Sort = string.Format("{0} ASC", name);
                _isTrue = true;
            }
            CollectionPager.PageSize = ConstantSetup.GovOfficeRegionRowPerPage;
            CollectionPager.BindToControl = RepeaterListGovOfficeRegion;
            CollectionPager.DataSource = _dataView;
            RepeaterListGovOfficeRegion.DataSource = CollectionPager.DataSourcePaged;
            RepeaterListGovOfficeRegion.DataBind();
        }
        /// <summary>
        /// Handles the Click event of the Button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "MyKey", "showmessage('Action successful .Page will redirect in <b> 3 </b>s','THÔNG BÁO','Default.aspx','true');", true);  
        }
    }
}
